package cn.shengchao.dubbo.bi;

import cn.hutool.core.collection.CollUtil;
import cn.shengchao.examstar.rpc.base.PageResult;
import cn.shengchao.examstar.distribute.infrastrure.po.DistributeDeptInfo;
import cn.shengchao.examstar.distribute.infrastrure.service.DistributeDeptInfoService;
import cn.shengchao.examstar.distribute.infrastrure.service.DistributeDeptUserRoleBindService;
import cn.shengchao.examstar.rpc.distribute.req.UserDbIQueryPage;
import cn.shengchao.examstar.rpc.distribute.resp.UserDbIResp;
import cn.shengchao.examstar.rpc.distribute.enums.DistributeDataStatusEnum;
import cn.shengchao.examstar.rpc.distribute.enums.SysRoleDistributeBindEnum;
import cn.shengchao.examstar.rpc.uddsys.bi.PlatformDbiUserBI;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collections;

/**
 * @program: examstarapi
 * @description: 系统用户体系的分销整合扩展
 * @author: Xiong weiqiang
 * @create: 2025-04-02 15:04
 */
@DubboService
public class PlatformDbiUserBIService implements PlatformDbiUserBI {
    @Autowired
    private DistributeDeptUserRoleBindService distributeDeptUserRoleBindService;
    @Autowired
    private DistributeDeptInfoService distributeDeptInfoService;
    @Override
    public PageResult<UserDbIResp> listUserByDept(UserDbIQueryPage userDbIQueryPage) {

            DistributeDeptInfo distributeDeptInfo=distributeDeptInfoService.getOne(
                    Wrappers.lambdaQuery(DistributeDeptInfo.class)
                            .eq(DistributeDeptInfo::getDeptId,userDbIQueryPage.getDeptId())
                            .eq(DistributeDeptInfo::getDataStatus, DistributeDataStatusEnum.STATUS_CURT.getStatus())
            );

            if (distributeDeptInfo==null){
                return PageResult.of(0L, Collections.emptyList());
            }
            PageResult<UserDbIResp> pageResult= distributeDeptUserRoleBindService.deptUserPageQuery(userDbIQueryPage);
            if (CollUtil.isNotEmpty(pageResult.getList())){

                    pageResult.getList().forEach(userDbIResp -> {
                        SysRoleDistributeBindEnum sysRoleDistributeBindEnum= SysRoleDistributeBindEnum.getByDistributeCode(userDbIResp.getDistributeRoleCode());
                        userDbIResp.setRoleIds(Collections.singletonList(sysRoleDistributeBindEnum.getSysRoleId()));
                        userDbIResp.setRoleNames(Collections.singletonList(sysRoleDistributeBindEnum.getDistributeName()));
                    });
            }
            return pageResult;
    }
}